﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;// DLL
using CppCli;

namespace app
{
    public partial class From : Form
    {   
        String context = "";// 产品版本标识
        String inputLine = "";//用户输入行，此全局变量的作用是实时显示用户输入的

        List<string> historyInput=new List<string>();// 保存用户输入
        string lastInput = "";
        int it;// 遍历用户输入的迭代器

        public CLI_VirtualDisk dll_VirtualDisk;
        public double VERSION = 1.2;

        public From()
        {
            InitializeComponent();
            CmdLineBegin();// begin
        }

        private void inputBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (inputBox.Text == "")
                {
                    ShowLastLine("");
                    return;
                }
                DealCmd(inputBox.Text);

                if (inputBox.Text != lastInput)// 和上次输入一样就没必要保存
                {
                    lastInput = inputBox.Text;// 记录本次输入方便下次比较
                    historyInput.Add(inputBox.Text);// 保存历史输入                    
                }
                it = historyInput.Count;// 将迭代器置为容器底部

                inputBox.Clear();

            }
            if (e.KeyCode == Keys.Up)
            {
                if (historyInput.Count != 0 && it > 0)
                {
                    inputBox.Text = historyInput[--it];
                    //inputBox.Select(inputBox.TextLength, 0);//重定位光标到输入框的最后方
                }
            }else if (e.KeyCode == Keys.Down)
            {
                if (historyInput.Count != 0 && it < historyInput.Count-1)
                {
                    inputBox.Text = historyInput[++it];
                    //inputBox.Select(inputBox.TextLength, 0);
                }
            }
            else if (e.KeyCode == Keys.Escape)
            {
                System.Environment.Exit(0);
            }
        }

        private void inputBox_TextChanged(object sender, EventArgs e)
        {
            ShowLastLine(inputBox.Text);
        }

        public void DealCmd(String input)// cmd执行
        {
            if (input.Length != 0)
            {
                if (input == "formatDisk")
                {
                    dll_VirtualDisk.formatDisk();
                }

                else if (input == "exit" || input == "quit")
                {
                    System.Environment.Exit(0);
                }
                else if (input == "cls")
                {
                    listBox1.Items.Clear();
                    listBox1.Items.Add("  ");
                    ShowLastLine("");
                }
                else
                {
                    dll_VirtualDisk.executeCmd(input);
                    ListBoxShow(dll_VirtualDisk.getOutput());// 打印结果
                }
            }
        }

        public void CmdLineBegin()
        {
            context += "Virtual Disk [版本 ";
            context += VERSION.ToString();
            context += "]\n";
            context += "(c) H3D Corporation。保留所有权力。\n\n";


            dll_VirtualDisk = new CLI_VirtualDisk();
            if (dll_VirtualDisk.isVaild==-2)
            {
                context += "VirtualDisk.dll导入失败!";
            }
            else if (dll_VirtualDisk.isVaild == -1)
            {
                context += "VirtualDisk对象实例化失败!";
            }
            else
            {
                ListBoxShow(context);
            } 
        }
        public void ListBoxShow(string str)// 因为是listBox组件，所以要按回车进行切割显示
        {
            String[] strArr = str.Split('\n');
            for (int i = 0; i < strArr.Length; i++)
            {
                listBox1.Items.Add(strArr[i]);
            }

            ShowLastLine("");
        }
        public void ShowLastLine(string input)// 始终打印最后一行(用户输入行)，而且是随输出动态变化的
        {
            int count = listBox1.Items.Count;
            listBox1.Items.RemoveAt(count-1);// 清空最后一行方便重写
            string res=dll_VirtualDisk.getCurPath() + ">";// 工作路径

            res += input;
            listBox1.Items.Add(res);// 工作路径+输入的内容

            if (listBox1.Items.Count > 0)
            {
                listBox1.SetSelected(listBox1.Items.Count - 1, true);// 始终选中最下一行
            }
        }
    }
}
